﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHTU;
using DevComponents.DotNetBar;

namespace NOMFinalSettlement.AppForm.Program
{
    public partial class fApplyProfile : Office2007Form
    {
        FinalSettlementModel.TableCanBo examine = null;
        private FinalSettlementModel.TableHoSo profileDB = null;

        public int idSelected = -1;

        public fApplyProfile()
        {
            InitializeComponent();
        }

        private void fApplyProfile_Load(object sender, EventArgs e)
        {
            doEnableControl(false);

            // Khởi tạo lớp model của TableCanBo
            examine = new FinalSettlementModel.TableCanBo();
            examine.EventLoadData += new FinalSettlementModel.TableCanBo.LoadData(OnLoadExamineData);            
            loadDataListUser();
            
            // Khởi tạo lớp model của TblHoSo
            profileDB = new FinalSettlementModel.TableHoSo();
            profileDB.EventLoadData += new FinalSettlementModel.TableHoSo.LoadData(OnLoadProfileData);
            doLoadDataApply();            
        }

        void OnLoadProfileData()
        {
            ISynchronizeInvoke i = (ISynchronizeInvoke)this;
            if (i.InvokeRequired)
            {
                FinalSettlementModel.TableHoSo.LoadData tmpDelegate = new FinalSettlementModel.TableHoSo.LoadData(OnLoadProfileData);
                i.BeginInvoke(tmpDelegate, null);
                return;
            }
            //Hàm xử lý
            doLoadDataApply();
        }
        void OnLoadExamineData()
        {
            ISynchronizeInvoke i = (ISynchronizeInvoke)this;
            if (i.InvokeRequired)
            {
                FinalSettlementModel.TableCanBo.LoadData tmpDelegate = new FinalSettlementModel.TableCanBo.LoadData(OnLoadExamineData);
                i.BeginInvoke(tmpDelegate, null);
                return;
            }
            //Hàm xử lý
            loadDataListUser();
        }
        #region Sự kiện
        private void btnAcceptUser_Click(object sender, EventArgs e)
        {
            if (listUser.Text == "" || listUser.SelectedValue == null)
            {
                MsgBox.Show("Vui lòng nhập thông tin cán bộ thẩm tra.", "Thông báo", NHTU.MsgBoxButton.Ok, NHTU.MsgBoxIcon.Stop);
                return;
            }
            if (idSelected > 0)
            {
                profileDB.updateExamine(idSelected, (int)listUser.SelectedValue);
                doEnableControl(false);
            }
        }

        private void menuCancel_Click(object sender, EventArgs e)
        {
            if (idSelected > 0)
            {
                profileDB.updateExamine(idSelected, 1);
                doEnableControl(false);
            }
        }

        private void dataViewNewDoc_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            idSelected = MainApp.Self.appProcess.getIdGrid(dataViewNewDoc, e, ctMenu);

            if (idSelected < 1)
            {
                doEnableControl(false);
            }
            else
            {
                //Lấy dữ liệu từ grid ra text box
                DataGridViewSelectedRowCollection selectRows = dataViewNewDoc.SelectedRows;
                foreach (DataGridViewRow row in selectRows)
                {
                    if (row.Cells[0].Value != null)
                    {
                        doEnableControl(true);
                        txtProjectName.Text = row.Cells[3].Value.ToString();
                        txtProjectCode.Text = row.Cells[1].Value.ToString();
                        cbbGroupProject.Text = row.Cells[4].Value.ToString();
                        cbbAcceptInvestment.Text = row.Cells[5].Value.ToString();
                        cbbInvestor.Text = row.Cells[6].Value.ToString();
                        dateAddDoc.Text = row.Cells[7].Value.ToString();
                        totalProject.Text = row.Cells[8].Value.ToString();
                        totalEstimates.Text = row.Cells[9].Value.ToString();
                        totalSettlement.Text = row.Cells[10].Value.ToString();
                        cbbConstructName.Text = row.Cells[11].Value.ToString();
                        if (row.Cells[2].Value.ToString() != "Chưa giao")
                        {
                            listUser.Text = row.Cells[2].Value.ToString();
                        }
                    }
                }                
            }
        }

        #endregion

        #region "hàm xử lý"
        void doLoadDataApply()
        {
            dataViewNewDoc.Rows.Clear();
            DataTable dt = profileDB.ListAllProfileData();

            foreach (DataRow row in dt.Rows)
            {
                if (dataViewNewDoc.ColumnCount>0)
                {
                dataViewNewDoc.Rows.Add(row["ID"], row["MaDuAn"], row["CanBo"], row["TenDuAn"],
                    row["NhomCongTrinh"], row["QuyetDinhDauTu"], row["ChuDauTu"], row["NgayNhan"],
                    row["TongDuAn"], row["TongDuToan"], row["DeNghiQuyetToan"], row["HinhThucChonThau"]);
                }
            }
        }

        void loadDataListUser()
        {
            listUser.DataSource = examine.ListOfficerData();
            listUser.DisplayMember = "CanBo";
            listUser.ValueMember = "ID";
        }
        #endregion

        # region Hàm dùng chung        
        void doEnableControl(bool yes)
        {
            btnAcceptUser.Enabled = yes;
        }
        #endregion
      
    }
}
